﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Http;

using ERPStore.Extensions;

using CloudSoft.Extensions;

namespace ERPStore.Web.Controllers
{
	[System.Web.Http.Description.ApiExplorerSettings(IgnoreApi = true)]
	public class MediaApiController : ApiController
	{
		public MediaApiController(ERPStore.Services.IDocumentService documentService,
			ERPStore.Services.ICatalogService catalogService)
		{
			this.DocumentService = documentService;
			this.CatalogService = catalogService;
		}

		protected ERPStore.Services.IDocumentService DocumentService { get; private set; }
		protected ERPStore.Services.ICatalogService CatalogService { get; private set; }

		public DateTime Ping()
		{
			return DateTime.Now;
		}

		[ActionFilters.ApiAuthorizedOperation]
		public ERPStore.Models.Media GetMediaById(string id)
		{
			var result = DocumentService.GetByCode(id);

			return result;
		}

		public IEnumerable<ERPStore.Models.Media> GetDocumentListByProductCode(string id)
		{
			if (id.IsNullOrTrimmedEmpty())
			{
				return null;
			}
			var product = CatalogService.GetProductByCode(id);
			if (product == null)
			{
				return null;
			}
			var result = DocumentService.GetMediaList(product);

			return result;
		}
	}
}
